package com.lintcode.solution;

public class Solution_428 {
    public static void main(String[] args) {
        double x = 2;
        int n = - 2147483648;
        System.out.println(Math.pow(x, n));
        long a = System.currentTimeMillis();
        System.out.println(myPow(x, n));
        long b = System.currentTimeMillis();
        System.out.println(b - a);
        
        long c = System.currentTimeMillis();
        System.out.println(myPow1(x, n));
        long d = System.currentTimeMillis();
        System.out.println(d - c);
    }
    
    public static double myPow(double x, int n) {
        if (n == 0) {
            return 1;
        }
        if (n == 1) {
            return x;
        }
        if (n < 0) {
            if (Integer.MIN_VALUE == n) {
                return 0;
            }
            n = - n;
            double a = myPow(x, n / 2);
            return 1 / (a * a * (n % 2 == 0 ? 1 : x));
        } else {
            double a = myPow(x, n / 2);
            return a * a * (n % 2 == 0 ? 1 : x);
        }
    }
    
    public static double myPow1(double x, int n) {
        double result = 1;
        if (n < 0) {
            for (int i = 0; i < - n; i++) {
                result *= x;
            }
            return 1 / result;
        } else {
            for (int i = 0; i < n; i++) {
                result *= x;
            }
            return result;
        }
        
    }
}
